package com.qf.qfwemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.qfleadnewscommons.mvc.ThreadLocalUtils;
import com.qf.qfleadnewsmodel.commons.dtos.PageRequestDto;
import com.qf.qfleadnewsmodel.commons.dtos.PageResponseResult;
import com.qf.qfleadnewsmodel.commons.dtos.ResponseResult;
import com.qf.qfleadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.qfleadnewsmodel.wemedia.dtos.NewsDto;
import com.qf.qfleadnewsmodel.wemedia.dtos.NewsListDto;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmNews;
import com.qf.qfwemedia.service.WmNewsService;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@RestController
@RequestMapping("/api/v1/news")
public class NewsController {

    @Autowired
    private WmNewsService wmNewsService;

    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody NewsDto newsDto) throws UnsupportedEncodingException {

        return wmNewsService.submit(newsDto);
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody NewsListDto newsListDto){

        newsListDto.checkParam();

        Long uid = ThreadLocalUtils.getUid();

        Integer channelId = newsListDto.getChannelId();
        Integer status = newsListDto.getStatus();
        Date beginPubDate = newsListDto.getBeginPubDate();
        Date endPubDate = newsListDto.getEndPubDate();
        String keyword = newsListDto.getKeyword();

        IPage<WmNews> page = new Page<>(newsListDto.getPage(),newsListDto.getSize());
        Wrapper<WmNews> qw = Wrappers.lambdaQuery(WmNews.class)
                .eq(WmNews::getUserId,uid)
                //根据频道id查询
                .eq(channelId!=null,WmNews::getChannelId,channelId)
                //根据发布状态查询
                .eq(status!=null,WmNews::getStatus,status)
                //根据发布时间查询
                .between(beginPubDate!=null&&endPubDate!=null,WmNews::getPublishTime,beginPubDate,endPubDate)
                //根据文章标题进行模糊匹配（单个自媒体人而言，数据量不会特别大，使用数据库即可）
                .like(!StringUtils.isBlank(keyword),WmNews::getTitle,keyword)
                //按照发布时间排序
                .orderByDesc(WmNews::getPublishTime);

        wmNewsService.page(page,qw);

        PageResponseResult result = new PageResponseResult(page.getCurrent(),page.getSize(),page.getTotal());
        result.setData(page.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());

        return result;
    }

    @GetMapping("/one/{id}")
    public ResponseResult edit(@PathVariable("id") Integer id){
        WmNews wmNews = wmNewsService.getById(id);
        return ResponseResult.okResult(wmNews);
    }
}
